////////////////////////////////////////////////////////////////////////////////
/// @file    ex6800led.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _EX6800LED_C_

// Files includes

#include "stdio.h"
#include "hal_conf.h"
#include "delay.h"
#include "oled.h"
#include "hal_fsmc.h"
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup EX6800LED
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup EX6800LED_Exported_Constants
/// @{



char printBuf[100];
void OLED_ShowString(u8 x, u8 y, const u8* p, u8 size);
////////////////////////////////////////////////////////////////////////////////
/// @brief  Initialize FSMC
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void FSMC_Init_6800(void)
{
    FSMC_InitTypeDef  FSMC_InitStructure;
    FSMC_NORSRAM_Bank_InitTypeDef  FSMC_BankInitStructure;

    FSMC_NORSRAM_BankStructInit(&FSMC_BankInitStructure);
    FSMC_NORSRAMStructInit(&FSMC_InitStructure);

    RCC_AHB3PeriphClockCmd(RCC_AHB3ENR_FSMC, ENABLE);                           //enable FSMCEN

#define USING96MHZ    // USINGHSI8MHZ 
#if defined(USINGHSI8MHZ)
    FSMC_BankInitStructure.FSMC_SMReadPipe    = 0;
    FSMC_BankInitStructure.FSMC_ReadyMode     = 0;
    FSMC_BankInitStructure.FSMC_WritePeriod   = 0x1F;
    FSMC_BankInitStructure.FSMC_WriteHoldTime = 1;
    FSMC_BankInitStructure.FSMC_AddrSetTime   = 3;
    FSMC_BankInitStructure.FSMC_ReadPeriod    = 0x1;
    FSMC_BankInitStructure.FSMC_DataWidth     = FSMC_DataWidth_16bits;
    FSMC_NORSRAM_Bank_Init(&FSMC_BankInitStructure, FSMC_NORSRAM_BANK0);
#endif
#if defined(USING96MHZ)// for 96M HSI PLL Trying
//  Only for write and read external DEVICE operations. 0: Internal FSMC 1: External DEVICE(i.e. from FSMC_NWAIT)
    FSMC_BankInitStructure.FSMC_SMReadPipe    = 0;                            // SM_read_pipe [1:0] the cycle of latch read data, namely the cycle of hready_RESP pull up
    FSMC_BankInitStructure.FSMC_ReadyMode     = 0;                            // Select whether the Hready_RESP signal comes from inside the FSMC IP or from an external DEVICE.
    FSMC_BankInitStructure.FSMC_WritePeriod   = 7;                            // Write operation cycle, configuration large continuous write will have an exception
    FSMC_BankInitStructure.FSMC_WriteHoldTime = 0;                            // The address of the write operation and the retention time of the data
    FSMC_BankInitStructure.FSMC_AddrSetTime   = 1;                            // Address setup time
    FSMC_BankInitStructure.FSMC_ReadPeriod    = 9;                            // Read operation cycle
    FSMC_BankInitStructure.FSMC_DataWidth     = FSMC_DataWidth_16bits;
    FSMC_NORSRAM_Bank_Init(&FSMC_BankInitStructure, FSMC_NORSRAM_BANK0);
#endif

    FSMC_InitStructure.FSMC_Mode = FSMC_Mode_6800;
    FSMC_InitStructure.FSMC_TimingRegSelect = FSMC_TimingRegSelect_0;
    FSMC_InitStructure.FSMC_MemSize = FSMC_MemSize_64MB;
    FSMC_InitStructure.FSMC_MemType = FSMC_MemType_NorSRAM;
    FSMC_InitStructure.FSMC_AddrDataMode = FSMC_AddrDataMUX;


    FSMC_NORSRAMInit(&FSMC_InitStructure);

}


////////////////////////////////////////////////////////////////////////////////
/// @brief  NORFLASH nReset GPIO configuration
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void OLED_nRST_Pin_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    //set PC6 as NORFLASH_nRST Pin
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Reset NORFLASH Controler
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void OLED_nRST_Setting(void)
{
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
    DELAY_Ms(1);
    GPIO_ResetBits(GPIOC, GPIO_Pin_6);
    DELAY_Ms(5);
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
    DELAY_Ms(120);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Initilaze OLED GPIO
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void OLED_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD | RCC_AHBENR_GPIOE, ENABLE);//RCC_AHBENR_GPIOB | RCC_AHBENR_GPIOC | RCC_AHBENR_GPIOA |
    // Use 6800 parallel port mode
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource3, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_12);


    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);







}
////////////////////////////////////////////////////////////////////////////////
/// @brief   FSMC_Ex6800OLED  function display
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void FSMC_Ex6800OLED_Demo(void)
{
    u8 t = 0;
    u8* p = (u8*)0x60080000;

    OLED_nRST_Pin_Config();
    OLED_GPIO_Init();
    FSMC_Init_6800();
    OLED_nRST_Setting();
    OLED_Init();

    OLED_Fill(0, 0, 127, 63, 0xFF);
    OLED_Clear();

    OLED_ShowString(5, 0, "Mindmotion", 16);
    OLED_ShowString(5, 24, "0.96' OLED TEST", 16);
    OLED_ShowString(5, 40, "MM32 2020/12/10", 12);
    OLED_ShowString(5, 52, "ASCII:", 12);
    OLED_ShowString(64, 52, "CODE:", 12);
    OLED_Refresh_Gram();  //Update display to OLED
    t = ' ';

    while(1) {
        DELAY_Ms(50);

        OLED_WR_Byte (0xb0, OLED_DATA);      // Set page address (0~7)
        OLED_WR_Byte (0x00, OLED_DATA);      // Set display location - column low address
        OLED_WR_Byte (0x10, OLED_DATA);      // Set display location - column height address
        printBuf[1] = *p;
        printBuf[2] = *p;
        printBuf[3] = *p;
        printBuf[4] = 0;
        printf("%s\n", (u8*)printBuf);
        DELAY_Ms(1);
        OLED_ShowChar(36, 52, t, 12, 1);  // Displays ASCII characters
        OLED_ShowNum(94, 52, t, 3, 12);  // Displays ASCII character code values
        OLED_Refresh_Gram();
        t++;
        if(t > '~')t = ' ';


    }
}

/// @}

/// @}

/// @}
